package com.qfjy.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.mapper.MeetingPubMapper;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class MeetingPubServiceImpl implements MeetingPubService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MeetingPubMapper meetingPubMapper;
    @Override
    public int insertSelective(MeetingPub meetingPub) {
        Integer uid = meetingPub.getUid();
        String key = "meeting:pub:add:"+String.valueOf(uid);
        if (redisTemplate.opsForValue().get(key) == null) {
            // 第一次进入,进行添加
            meetingPub.setId(String.valueOf(UUID.randomUUID()));
            // 会议编号
            //参数1为终端ID
            //参数2为数据中心ID
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            String pcode = snowflake.nextIdStr();
            meetingPub.setPcode(pcode);
            meetingPub.setCreateDate(new Date());
            meetingPub.setStatus((short) 1);
            redisTemplate.opsForValue().set(key,uid.toString(),10, TimeUnit.MINUTES);
            return meetingPubMapper.insertSelective(meetingPub);
        } else {
            // 第二次进入
            return -1;
        }
    }

    @Override
    public List<MeetingPub> selectListMeetingPubByUid(int uid) {
        return meetingPubMapper.selectListMeetingPubByUid(uid);
    }

    @Override
    public List<MeetingPub> selectMeetingPubGrabListByUidOrTid(String uid, String tid) {
        return meetingPubMapper.selectMeetingPubGrabListByUidOrTid(uid,tid);
    }

}
